package com.sgb.knowledgeshare.view.fragment;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareUserListBinding;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.knowledgeshare.view.adapter.ShareUserListAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareUserListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.VideoListEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ShareUserListFragment extends AppLazyFragment {

    private static String SHARETYPE = "share_type";
    private static String USER_ID = "user_id";

    private FragmentShareUserListBinding binding;
    ShareUserListModel shareUserListModel;
    private int UserShareType;
    private boolean isLoadMore = false;
    private int page;
    private String userid;
    private ShareUserListAdapter shareuserlistadapter;
    List<VideoBean> list;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private int pages;

    public static ShareUserListFragment newInstance(String userId, int type) {
        ShareUserListFragment fragment = new ShareUserListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(SHARETYPE, type);
        bundle.putString(USER_ID, userId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_user_list, container, false);
        shareUserListModel = new ViewModelProvider(this).get(ShareUserListModel.class);
        shareUserListModel.setActivity(ShareUserListFragment.this);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    protected void onLazyLoad() {
        getdata();
    }

    @Override
    public void onVisible() {
        super.onVisible();
        getdata();
    }

    protected void initView() {
        if (getArguments() != null) {
            UserShareType = getArguments().getInt(SHARETYPE);
            userid = getArguments().getString(USER_ID);
        }
        if (shareuserlistadapter == null) {
            shareuserlistadapter = new ShareUserListAdapter(getActivity(), null);
        }
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(shareuserlistadapter);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(getContext())
                .setHorizontal(R.dimen.dp10)
                .setVertical(R.dimen.dp10)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        binding.lrecyclerview.setLayoutManager(layoutManager);
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                shareuserlistadapter.clearDatas();
                mLRecyclerViewAdapter.notifyDataSetChanged();
                getdata();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                if (page <= pages) {
                    shareUserListModel.getVideoListData(page, userid,UserShareType + "");
                }else {
                    binding.lrecyclerview.setNoMore(true);
                }
            }
        });
//    条目的点击事件
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), PlayListActivity.class);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) list);
                intent.putExtra(PlayListActivity.PSO, position);
                intent.putExtra(PlayListActivity.VIDEOTYPE, "fx_mine");
                intent.putExtra(PlayListActivity.PAGE, page);
                intent.putExtra(PlayListActivity.USERNO, userid + "");
                intent.putExtra(PlayListActivity.VIDEOTITLE, UserShareType + "");
                getActivity().startActivity(intent);
            }
        });

    }

    private void getdata() {
        if (!TextUtils.isEmpty(userid)) {
            shareuserlistadapter.setDatas(new ArrayList<>());
            isLoadMore = false;
            page = 1;
            shareUserListModel.getVideoListData(page, userid,UserShareType + "");
        }
    }

    //    获取数据列表
    public void setVideoListData(VideoListEntity data) {
        if (!isAdded()) return;
        List<VideoBean> records = data.getRecords();
        pages = data.getPages();
        if (records == null) {
            shareuserlistadapter.setDatas(new ArrayList<>());
            return;
        }
        binding.lrecyclerview.refreshComplete(records.size());
        if (isLoadMore) {
            if (records.size() > 0) {
                shareuserlistadapter.addDatas(records);
            } else {

                MToast.showToast(getContext(), "没有视频啦!");
            }
        } else {
            if (records.size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                shareuserlistadapter.setDatas(records);
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                shareuserlistadapter.setDatas(null);
            }
        }
        this.list = shareuserlistadapter.getDatas();
    }

}

